package com.xiaoshuidi.cloud.module.bpm.job.xxljob;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.swo.vo.TicketDelayJobParamDTO;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.swo.SwoTicketDO;
import com.xiaoshuidi.cloud.module.bpm.dal.mysql.swo.SwoTicketMapper;
import com.xiaoshuidi.cloud.module.bpm.enums.swo.SwoServiceTypeEnum;
import com.xiaoshuidi.cloud.module.bpm.enums.swo.TicketServiceStatusEnum;
import com.xiaoshuidi.cloud.module.bpm.job.delayjob.TicketDelayJobService;
import com.xiaoshuidi.cloud.module.bpm.job.delayjob.TicketDelayJobTypeEnum;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@TenantJob
public class RepairTimeoutReminderJob {

    @Resource
    private SwoTicketMapper swoTicketMapper;
    @Resource
    private TicketDelayJobService ticketDelayJobService;

    /**
     * 筛选出明天到期的维修工单, 未完成的加入发送短信提醒的延迟任务中
     */
    @XxlJob("repair-timeout-reminder")
    public void repairTimeoutReminder() {
        log.info("筛选出明天到期的维修工单, 未完成的加入发送短信提醒的延迟任务中");
        LambdaQueryWrapper<SwoTicketDO> wrapper = Wrappers.lambdaQuery();
        // 服务类型是维修
        wrapper.eq(SwoTicketDO::getServiceType, SwoServiceTypeEnum.MAINTENANCE.getValue());
        // 状态是进行中
        wrapper.eq(SwoTicketDO::getStatus, TicketServiceStatusEnum.IN_PROGRESS.getStatus());
        // 时间是今天
        wrapper.between(SwoTicketDO::getServiceTimeEnd,
                LocalDateTime.of(LocalDate.now(), LocalTime.MIN),
                LocalDateTime.of(LocalDate.now(), LocalTime.MAX));
        List<SwoTicketDO> swoTicketDOS = swoTicketMapper.selectList(wrapper);

        log.info("今天:{}结束的进行中的任务:{}", LocalDate.now(), JsonUtils.toJsonString(swoTicketDOS));
        swoTicketDOS.stream().forEach(swoTicketDO -> {
            // 当前时间到任务结束前30分钟时间相差的分钟数
            // 当前时间在任务结束时间之后，不需要提醒
            long minutes = 0;
            if(LocalDateTime.now().isAfter(swoTicketDO.getServiceTimeEnd())){
                return;
            }
            if(LocalDateTime.now().isBefore(swoTicketDO.getServiceTimeEnd().plusMinutes(-30))){
            // 当前时间在任务结束时间的30分钟之前
                minutes = Duration.between(LocalDateTime.now(), swoTicketDO.getServiceTimeEnd().plusMinutes(-30)).toMillis();
            }else {
                minutes = 1;
            }
            log.info("工单:{},到期时间:{},{}分钟后执行", swoTicketDO.getId(), swoTicketDO.getServiceTimeEnd(), minutes);
            TicketDelayJobParamDTO ticketDelayJobParamDTO = new TicketDelayJobParamDTO()
                    .setSwoTicketId(swoTicketDO.getId())
                    .setAssigneeId(swoTicketDO.getAssignee());
            // 添加延迟任务, 在指定时间后执行, 检查任务是否完成, 未完成则需要短信提醒
            ticketDelayJobService.addDelayJob(TicketDelayJobTypeEnum.REPAIR_TIMEOUT_REMINDER, ticketDelayJobParamDTO, minutes, TimeUnit.MINUTES);
        });
        log.info("工单筛选任务结束");
    }
}
